package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.MobileUser;
import com.ruoyi.business.service.IMobileUserService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * @Author liumaotong
 * @Description 小程序用户Controller
 * @Date 2024/6/13
 */
@RestController
@RequestMapping("/mobile/user")
public class MobileUserController extends BaseController {

    @Autowired
    private IMobileUserService mobileUserService;

    /**
     * @Author liumaotong
     * @Description 判断是否需要登录，已经登录过的用户无需再次登录
     * @Date 2024/6/13
     * @Param [user]
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @RequestMapping(value = "/isLogin",method = RequestMethod.POST)
    public AjaxResult isLogin(@RequestBody MobileUser user){
        return AjaxResult.success(mobileUserService.isLogin(user));
    }


    /**
     * @Author liumaotong
     * @Description 用户登录
     * @Date 2024/6/13
     * @Param [user]
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    public AjaxResult userLogin(@RequestBody MobileUser user){
        return AjaxResult.success(mobileUserService.userLogin(user));
    }

    /**
     * 查询小程序用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MobileUser mobileUser) {
        startPage();
        List<MobileUser> list = mobileUserService.selectMobileUserList(mobileUser);
        return getDataTable(list);
    }

    /**
     * 导出小程序用户列表
     */
    @Log(title = "小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MobileUser mobileUser) {
        List<MobileUser> list = mobileUserService.selectMobileUserList(mobileUser);
        ExcelUtil<MobileUser> util = new ExcelUtil<MobileUser>(MobileUser.class);
        util.exportExcel(response, list, "小程序用户数据");
    }

    /**
     * 获取小程序用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mobileUserService.selectMobileUserById(id));
    }

    /**
     * 新增小程序用户
     */
    @Log(title = "小程序用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MobileUser mobileUser) {
        return toAjax(mobileUserService.insertMobileUser(mobileUser));
    }

    /**
     * 修改小程序用户
     */
    @Log(title = "小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MobileUser mobileUser) {
        return toAjax(mobileUserService.updateMobileUser(mobileUser));
    }

    /**
     * 删除小程序用户
     */
    @Log(title = "小程序用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mobileUserService.deleteMobileUserByIds(ids));
    }

    /**
     * @Author liumaotong
     * @Description 小程序端 我的页面 用户详情
     * @Date 2024/6/14
     * @Param [openId]
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @RequestMapping(name = "/getUserByOpenId",method = RequestMethod.GET)
    public AjaxResult getUserByOpenId(@RequestParam("openId") String openId){
        return AjaxResult.success(mobileUserService.getUserByOpenId(openId));
    }

}
